package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.AnswerGroupService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/4/4
 */
@RestController
@RequestMapping("/group")
public class GroupController extends BaseController {
    @Resource
    private AnswerGroupService answerGroupService;

    /**
     * 导入分组(分配学生)
     *
     * @param file 分组信息文件
     */
    @PostMapping("/import")
    public Map<String, Object> importGroupInfo(MultipartFile file) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            answerGroupService.importGroupInfo(teacher.getName(), teacher.getCollegeName(), file);
            return null;
        });
    }

    /**
     * 查看分组信息
     */
    @GetMapping("/info")
    public Map<String, Object> groupInfo() {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            return answerGroupService.groupInfo(teacher.getCollegeName());
        });

    }

    /**
     * 添加记录员
     *
     * @param id            id
     * @param teacherNumber 教师编号
     */
    @PostMapping("/person/add")
    public Map<String, Object> addPerson(Long id, String teacherNumber) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            answerGroupService.addPerson(id, teacher.getCollegeName(), teacherNumber);
            return null;
        });
    }

    /**
     * 记录员查找自己负责的
     */
    @GetMapping("/student/info")
    public Map<String, Object> findByPerson() {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            return answerGroupService.findByPerson(teacher.getId());
        });
    }

    /**
     * 答辩过程记录
     */
    @PostMapping("/answer/record")
    public Map<String, Object> addAnswerRecord(String content, String url) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            answerGroupService.addAnswerRecord(teacher.getId(), content, url);
            return null;
        });
    }

    /**
     * 查找答辩记录
     */
    @GetMapping("/answer/record/show")
    public Map<String, Object> showAnswerRecord() {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            return answerGroupService.showAnswerRecord(teacher.getId());
        });
    }
}
